﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Windows.Forms;
using MesBSW.BLL.Mes;
using MesBSW.BLL.Role;
using MesBSW.DAL.MesDB;
using MesBSW.DAL.MesDTO;
using MesBSW.DAL.RoleDB;
using MesBSW.DAL.RoleDTO;
using Sunny.UI;
using Sunny.UI.Win32;

namespace MesBSW.View
{
    public partial class MainFrm : UIHeaderAsideMainFrame
    {
        /// <summary>
        /// 构造方法
        /// </summary>
        /// <param name="user"></param>
        /// <param name="lang"></param>
        public MainFrm(BaseUserDTO user, LanguageInfo lang)
        {
            InitializeComponent();
            SetLoginUser(user);
            SetUseLanguage(lang);
        }


        #region "内部变量"
        private static BaseUserDTO _login_user = null;
        private List<BaseMenuDTO> menuList = new List<BaseMenuDTO>();
        private HomeControler myCtrl = new HomeControler();
        private LanguageControler langCtrl = new LanguageControler();
        private static BaseProgModel _base_prog = null;
        private static LanguageInfo _curr_lang = null;
        private static List<LanguageMulitDTO> _lang_mulit_list = new List<LanguageMulitDTO>();
        #endregion


        #region "属性"
        /// <summary>
        /// 本系统的系统信息
        /// </summary>
        public static BaseProgModel BaseProg { get { return _base_prog; } }

        /// <summary>
        /// 登陆人信息
        /// </summary>
        public static BaseUserDTO LoginUser
        {
            get { return _login_user; }
        }


        /// <summary>
        /// 当前用户选择的语言种类
        /// </summary>
        public static LanguageInfo CurrLanguage
        {
            get { return _curr_lang; }
        }

        /// <summary>
        /// 当前语言的所有项目设定
        /// </summary>
        public static List<LanguageMulitDTO> LanguageList
        {
            get { return _lang_mulit_list; }
            set { _lang_mulit_list = value; }
        }
        #endregion


        private void SetLoginUser(BaseUserDTO _user)
        {
            _login_user = _user;
        }


        private void SetUseLanguage(LanguageInfo _lang)
        {
            _curr_lang = _lang;
        }

        


        #region "控件事件"
        private void MainFrm_Shown(object sender, EventArgs e)
        {
            if (_curr_lang != null)
            {
                _lang_mulit_list = this.langCtrl.SearchLanguageMulitForType(_curr_lang);
            }

            if (_login_user != null)
            {
                this.menuList = new List<BaseMenuDTO>();
                var roleList = myCtrl.GetRoleInfoForUser(_login_user);
                if (roleList != null && roleList.Count() > 0)
                {
                    List<int> deptIds = new List<int>();
                    foreach (var role in roleList)
                    {
                        var tmpList = this.myCtrl.GetMenuTreeForRole(role);
                        if (tmpList != null)
                        {
                            foreach (var menu in tmpList)
                            {
                                if (this.menuList.Contains(menu) == false)
                                {
                                    this.menuList.Add(menu);
                                }
                            }
                        }

                        if (string.IsNullOrEmpty(role.depts_id) == false)
                        {
                            foreach (string tmpDept in role.depts_id.Split(','))
                            {
                                if (int.TryParse(tmpDept, out int tmpInt) == true && deptIds.Contains(tmpInt) == false)
                                {
                                    deptIds.Add(tmpInt);
                                }
                            }
                        }

                        if (deptIds != null && deptIds.Count > 0)
                        {
                            _login_user.UseDetpList = this.myCtrl.SearchDeptInfoForID(deptIds);
                        }
                    }
                }

                if (this.menuList != null && this.menuList.Count() > 0)
                {
                    this.InitLeftTree();//没法进入这个判断导致没数据
                }

                this.Refresh();
            }
            else
            {
                UIMessageBox.ShowError("权限读取失败！");
            }
        }
        #endregion

        #region "内部方法"
        /// <summary>
        /// 初始化左边的功能菜单
        /// </summary>
        private void InitLeftTree()
        {
            if (this.menuList != null && this.menuList.Count() > 0)
            {
                foreach (var menu in this.menuList)
                {
                    TreeNode parNode = null;
                    if (menu.class_name != null && menu.class_name.Length > 0)
                    {
                        Type tmpType = Type.GetType(menu.class_name);
                        if (tmpType.BaseType.Name.Equals("UIPage"))
                        {
                            var page = CreateInstance<UIPage>(menu.class_name);
                            page.Text = menu.menu_name;
                            parNode = this.Aside.CreateNode(AddPage(page, Convert.ToInt32(menu.PageIndex)));
                        }
                        else
                        {
                            parNode = this.Aside.CreateNode(menu.menu_name, Convert.ToInt32(menu.PageIndex));
                        }
                    }
                    else
                    {
                        parNode = this.Aside.CreateNode(menu.menu_name, Convert.ToInt32(menu.PageIndex));
                    }

                    if (menu.ChildMenu != null && menu.ChildMenu.Count() > 0)
                    {
                        foreach (var childMenu in menu.ChildMenu)
                        {
                            this.InitTreeNode(parNode, childMenu);
                        }
                    }
                }
            }
        }

        /// <summary>
        /// 初始化功能菜单下面的子菜单
        /// </summary>
        /// <param name="parMenu"></param>
        /// <param name="childMenu"></param>
        private void InitTreeNode(TreeNode parMenu, BaseMenuDTO childMenu)
        {
            if (childMenu != null && childMenu.class_name != null && childMenu.class_name.Length > 0)
            {
                Type tmpType = Type.GetType(childMenu.class_name);
                if (tmpType.BaseType.Name.Equals("UIPage")) 
                {
                    var childPage = CreateInstance<UIPage>(childMenu.class_name);
                    childPage.Text = childMenu.menu_name;
                    this.Aside.CreateChildNode(parMenu, 
                                    AddPage(childPage, Convert.ToInt32(childMenu.PageIndex)));
                }
                else
                {
                    this.Aside.CreateChildNode(parMenu, childMenu.menu_name, Convert.ToInt32(childMenu.PageIndex));
                }
            }
            else
            {
                this.Aside.CreateChildNode(parMenu, childMenu.menu_name, Convert.ToInt32(childMenu.PageIndex));
            }
        }

        
        #endregion

        /// <summary>
        /// 创建对象实例
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="fullName">命名空间.类型名</param>
        /// <returns></returns>
        public static T CreateInstance<T>(string fullName)
        {
            Type o = Type.GetType(fullName);
            dynamic obj = Activator.CreateInstance(o, true);
            return (T)obj;//类型转换并返回
        }

        private void MainFrm_Load(object sender, EventArgs e)
        {
            string progName = "贝斯旺MES系统";

            _base_prog = this.myCtrl.SearchProgInfoByName(progName);

            if (_base_prog == null)
            {
                UIMessageBox.ShowError("数据库中没有本系统的任何资料！");
                Application.Exit();
            }

            string ss = LoginUser.real_name;
            this.lblUserName.Text = LoginUser.user_name;
        }

        private void lblUserName_Click(object sender, EventArgs e)
        {
            if (ConfigurationManager.ConnectionStrings["MesDB"] == null) return;
            string mess = ConfigurationManager.ConnectionStrings["MesDB"].ToString();
            if (!string.IsNullOrEmpty(mess))
            {
                string[] messss = mess.Split(";");
                UIMessageBox.ShowInfo(string.Format("服务器:{0}\r\n数据库:{1}", messss[0].Split("=")[1], messss[1].Split("=")[1]));
            }
        }
    }
}
